﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SLBehaviourSystem
{
    [ActionComponent]
    public class TreeReferenceComponent : BehaviourComponent
    {
        int TreeId=-1;
        Dictionary<int, BehaviourTree> treeDict = new Dictionary<int, BehaviourTree>();
        BehaviourTree tree;
        protected override void OnInit()
        {
            base.OnInit();
            
        }
        protected override void OnBindNode()
        {
            base.OnBindNode();
            if (TreeId!=-1)
            {
                treeDict[TreeId].Interupt();
            }
            TreeId = node.BehaviourData.GetInt("TreeId");
            if (!treeDict.ContainsKey(TreeId))
            {
                treeDict.Add(TreeId, BehaviourSystem.Inst.CreateTree(TreeId, node.Agent));
                treeDict[TreeId].OnBehaviourTreeCompleted += ()=>
                {
                    OnExcuted(BehaviourNodeState.Success);
                };
            }
            
        }

        public override void ExcuteAsync(Action<BehaviourNodeState> onExcuteCompleted)
        {
            base.ExcuteAsync(onExcuteCompleted);
            treeDict[TreeId].Excute();
        }
    }
}
